#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<bitset>
#include<unordered_set>
#include<functional>
using namespace std;
int longestCycle(vector<int>& edges) {
	int n = edges.size();
	vector<int> in(n, 0);
	for (int i = 0; i < n; i++)
	{
		in[edges[i]]++;
	}
	queue<int> q;
	for (int i = 0; i < n; i++)
	{
		if (in[i] == 0)
			q.push(i);
	}
	while (!q.empty())
	{
		auto tmp = q.front();
		q.pop();
		if (--in[edges[tmp]] == 0)
		{
			q.push(edges[tmp]);
		}
	}
	int ret = 0;
	for (int i = 0; i < n; i++)
	{
		if (in[i] == 0)
			continue;
		in[i] = 0;
		int x = edges[i], len = 1;
		while (x != i)
		{
			in[x] = 0;
			x = edges[x];
			len++;
		}
		ret = max(ret, len);
	}
	return ret==0?-1:ret;
}
//vector<int> countVisitedNodes(vector<int>& edges) {
//	int n = edges.size();
//	vector<int> ret(n);
//	auto FindCount = [&](int x)
//	{
//		bitset<100000> visited;
//		visited[x] = true;
//		int count = 1;
//		for (int i = edges[x]; i != x && visited[i]==false; i = edges[i])
//		{
//			visited[i] = true;
//			count++;
//		}
//		ret[x] = count;
//	};
//	for (int i = 0; i < n; i++)
//		FindCount(i);
//	return ret;
//}
//vector<int> countVisitedNodes(vector<int>& edges) {
//	int n = edges.size();
//	vector<int> in(n),ret(n);
//	for (int i = 0; i < n; i++)
//	{
//		in[edges[i]]++;
//	}
//	queue<int> q;
//	unordered_set<int> branch;
//	for (int i = 0; i < n; i++)
//	{
//		if (in[i] == 0)
//			q.push(i);
//	}
//	while (!q.empty())
//	{
//		auto tmp = q.front();
//		q.pop();
//		branch.insert(tmp);
//		if (--in[edges[tmp]] == 0)
//			q.push(edges[tmp]);
//	}
//	function<int(int)> dfs = [&](int x)->int
//	{
//		int len = 0;
//		if (in[x] <= 0)
//		{
//			len = dfs(edges[x]) + 1;
//		}
//		return len;
//	};
//	for (auto& e : branch)
//	{
//		ret[e] = dfs(e);
//	}
//	int len = 1;
//	for (int i = 0; i < n; i++)
//	{
//		if (in[i] <= 0)
//			continue;
//		in[i] = 0;
//		for (int x = edges[i]; x != i && in[x]!= 0; x = edges[x])
//		{
//			in[x] = 0;
//			len++;
//		}
//	}
//	for (int i = 0; i < n; i++)
//	{
//		ret[i] += len;
//	}
//	return ret;
//}
vector<int> countVisitedNodes(vector<int>& edges) {
	int n = edges.size();
	vector<int> in(n),ret(n);
	for (int i = 0; i < n; i++)
	{
		in[edges[i]]++;
	}
	queue<int> q;
	for (int i = 0; i < n; i++)
	{
		if (in[i] == 0)
			q.push(i);
	}
	vector<vector<int>> rg(n);
	while (!q.empty())
	{
		auto tmp = q.front();
		q.pop();
		rg[edges[tmp]].push_back(tmp);
		if (--in[edges[tmp]] == 0)
			q.push(edges[tmp]);
	}
	function<void(int,int)> rdfs = [&](int x,int depth)
	{
		ret[x] = depth;
		for (auto& e : rg[x])
		{
			if (in[e] == 0)
				rdfs(e, depth + 1);
		}
	};
	for (int i = 0; i < n; i++)
	{
		if (in[i] <= 0)
			continue;
		in[i] = -1;
		int len = 1;
		vector<int> ring;
		ring.push_back(i);
		for (int x = edges[i]; i != x; x = edges[x])
		{
			ring.push_back(x);
			len++;
		}
		for (auto& e : ring)
		{
			rdfs(e, len);
		}
	}
	return ret;
}
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<bitset>
#include<unordered_set>
#include<functional>
using namespace std;
struct Node
{
	int value;
	Node* left;
	Node* right;
	Node(int x)
		:value(x),left(nullptr),right(nullptr)
	{}
};
Node* BuildTree(vector<int>::iterator inleft, vector<int>::iterator inright, vector<int>::iterator postleft, vector<int>::iterator postright)
{
	if (postleft >= postright)
		return nullptr;
	int value = *(postright - 1);
	Node* root = new Node(value);
	vector<int>::iterator mid = find(inleft, inright, value);
	root->left = BuildTree(inleft, mid, postleft, postleft + (mid - inleft));
	root->right = BuildTree(mid + 1, inright, postleft + (mid - inleft), postright - 1);
	return root;
}
void Preorder(Node* root)
{
	if (root == nullptr)
		return;
	cout << root->value << " ";
	Preorder(root->left);
	Preorder(root->right);
}
int main()
{
	int n;
	cin >> n;
	vector<int> inorder(n), postorder(n);
	for (int i = 0; i < n; i++)
		cin >> postorder[i];
	for (int i = 0; i < n; i++)
		cin >> inorder[i];
	Node* root = BuildTree(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
	cout << "Preorder:";
	Preorder(root);
	return 0;
}
